<?php

namespace app\bing\controller;

use app\bing\model\Mon;
use app\bing\validate\Mont;
use phpDocumentor\Reflection\Types\Iterable_;
use think\Controller;
use think\Db;
use think\facade\Cache;
use think\facade\View;
use think\Request;

class Month extends Controller
{
    //文章的添加功能
    public function create(Request $request)
    {
        if ($request->isPost()){
           $arr=$request->post();
           //表单validate验证
            $validate = new \app\bing\validate\Mont;
            $result = $validate->check($arr);
            if(!$result){
                return json(['code'=>201,'msg'=>$validate->getError()]) ;
            }
            //封面上传
            $file=$request->file('img');
            $info=$file->move('./uploads');
            $arr['img']='/uploads/'.$info->getSaveName();
            $arr['create_time']=time();
            $res=Mon::add($arr);
            if ($res){
                //文章添加成功提示
                $this->success('文章添加成功','show');
            }else{
                $this->error('文章添加失败','create');

            }
        }
        return View::fetch();
    }
    //文章列表管理
    public function show(Request $request)
    {
        $keyword=$request->param('keyword');
        if ($keyword){
            $data=Mon::sear( $keyword);
            return View::fetch('show',['data'=>$data]);
        }else{
            $data=Mon::list();
            return View::fetch('show',['data'=>$data]);
        }
    }

    /**
     * 显示编辑资源表单页.
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function edit($id)
    {
            $id=\request()->param();
           $data=Mon::info($id);
           return View::fetch('edit',['data'=>$data]);
    }
    //修改
    public function update(Request $request, $id)
    {
        $arr=$request->post();
        //表单validate验证
        $validate = new \app\bing\validate\Mont;
        $result = $validate->check($arr);
        if(!$result){
            return json(['code'=>201,'msg'=>$validate->getError()]) ;
        }
        //封面上传
        $file=$request->file('img');
        $info=$file->move('./uploads');
        $arr['img']='/uploads/'.$info->getSaveName();
        $arr['create_time']=time();
        $res=Mon::upda($arr,$id);
        if ($res){
            //文章添加成功提示
            $this->success('文章修改成功','show');
        }else{
            $this->error('文章修改失败','show');

        }
    }

    /**
     * 删除文章
     *
     * @param  int  $id
     * @return \think\Response
     */
    public function delete($id)
    {
        $id=\request()->param('id');
        $res=Mon::del($id);
        if ($res){
            $this->success('文章删除成功','show');
        }else{
            $this->error('文章删除失败','show');

        }
    }
    //软删除
    public function soft($id)
    {
        $id=\request()->param('id');
        $res=Mon::destroy($id);
        if ($res){
            $this->success('文章软删除成功','show');
        }else{
            $this->error('文章软删除失败','show');

        }
    }
    //管理员登陆
    public function login(){
        if (\request()->isPost()){
            $arr['name']=\request()->param('name');
            $arr['password']=\request()->param('password');
           $res=Db::name('admin')->where('name',$arr['name'])->whereOr('password',$arr['password'])->find();

           if ($res){
               cache('admin_id',$res['id']);//缓存
               $this->success('登录成功','create');
           }else{
               $this->error('登录失败');
           }

        }
        return View::fetch('login');
    }
    //退出登录有问题
    public function exitAdmin(){
        $admin_id=Cache::get('admin_id');
        unset($admin_id);
        echo "注销成功";
    }
}
